home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1930 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.3 KB  |  74 lines

  1. Path: news.cityu.edu.hk!95469487
  2. From: 95469487@cpccux0.cityu.edu.hk ()
  3. Newsgroups: comp.lang.c,comp.lang.c++,comp.lang.perl
  4. Subject: Re: Stupid array problems
  5. Followup-To: comp.lang.c,comp.lang.c++,comp.lang.perl
  6. Date: 14 Jan 1996 07:53:45 GMT
  7. Organization: City University of Hong Kong
  8. Message-ID: <4dacq9$a05@ctylnk.cityu.edu.hk>
  9. References: <4d9b9v$14n@paperboy.ids.net>
  10. NNTP-Posting-Host: cpccux1.cityu.edu.hk
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. scarney (scarney@conan.ids.net) wrote:
  14. : Ok, I've been having what I thought would be a simple problem but no one 
  15. : seems to quite figure out. I Have an array of strings, both the key and 
  16. : the string being pointers. Putting information onto the array is like a 
  17. : stack.  My problem comes in the removal of information from it. Popping 
  18. : from a stack only pulls the lastelement out of the array, I want to pull 
  19. : any element out. Destroying the data isn't the hard part because I just 
  20. : blow up the pointers...the problem is that the indexing for the array 
  21. : gets kind of screwy. If I have an array organized by integers with 
  22. : elements 1 2 3 4 5 6 7 8, if I decided to kill element #4 I'm going to 
  23. : have an array of 1 2 3 5 6 7 8, and as time goes on its going to get more 
  24. : screwy. Is there an easy way to delete an element while preserving some 
  25. : semblance of sequence in the array indexing? In Perl I'd use the splice() 
  26. : command...is there anything similar in C/C++?
  27.  
  28. : I get the feeling I'm looking at this from a completely wrong 
  29. : perspective, so don't flame me if I am...
  30.  
  31. : Thanks.
  32. : -Seth
  33.     I think a better way is to use a "link" rather than array. for example
  34. :
  35.  
  36. class node
  37. {
  38.     friend class array;
  39.     int index;
  40.     int foo;        // content
  41.     node *link;        // a pointer (important) pointed back to node again
  42. };
  43.  
  44. class array
  45. {
  46.     void add_item(int);
  47.     void delete_item(int);    // function you want
  48.     node *head;
  49. };
  50.  
  51. void array::delete_item(int index)    // input: which item you want to delete
  52. {
  53.     node *prev,*temp;
  54.     temp = head;
  55.     while (temp)
  56.     { if (temp->index == index) break;
  57.     else {prev=temp; temp=temp->link}}
  58.             
  59. if (temp != 0)
  60.     { prev->link=temp->link;
  61.     delete temp;}
  62. }
  63.  
  64.  
  65.  
  66. certainly, this example is not complete, you can look at your book to check
  67. it out. 
  68.     (0)-->(1)-->(2)-->.........-->(10)-->0
  69.  
  70.  
  71.  
  72.  
  73.         
  74.